
$(document).ready(function()
{

    var text = '马旭东 - 入戏太深.mp3';
    document.getElementById('an').onselectstart = function(){event.returnValue=false;}
    // 创建 play对象
    var myplayer = ListenMusic();
    var Storage = myplayer.getStorage();
    myplayer.src = text;



    // 播放按钮
    $('.control-status').click(function()
    {
        if( myplayer.paused )
        {
            $('.control-status>i').addClass('current');
            myplayer.play();
        }
        else
        {
            $('.control-status>i').removeClass('current');
            myplayer.pause();
        }
    });

    // http://s.geci.me/lrc/226/22642/2264296.lrc
    // http://s.geci.me/lrc/390/39042/3904222.lrc
    $.get('http://s.geci.me/lrc/390/39042/3904222.lrc', function(data)
    {
        myplayer.analysisLrc(data, true);
    });
    
    // 播放按钮透明效果
    $('.cover').hover(
        function()
        {
            $('.control-status').css('opacity','0.8');
        },
        function()
        {
            $('.control-status').css('opacity','0.2');
        }
    );

    /* 侧边收缩按钮 */
    $('.an-side').click(function()
    {
        var has = $('.an-side>i').hasClass('active');
        if( has )
        {
            // 收缩
            $('.an-side>i').removeClass('active');
            $('#an').removeClass('active');
        }
        else
        {
            // 伸展
            $('.an-side>i').addClass('active');
            $('#an').addClass('active');
        }
    });

    /* 控制按钮 - 模式切换 */
    $('.control-loop').click(function()
    {
        var icon  = '';
        var title = '';

        var nextIcon  = '';
        var nextTitle = '';

        var model = myplayer.model ? myplayer.model:2;
        console.log(model);

        switch(model)
        {
            case 1: // 列表循环
                icon  = 'fa-exchange';
                title = '列表循环';

                model     = 2; 
                nextIcon  = 'fa-random';
                nextTitle = '随机播放';
                break;
            case 2: // 随机播放
                icon = 'fa-random';
                title = '随机播放';

                model     = 3; 
                nextIcon  = 'fa-retweet';
                nextTitle = '单曲循环';
                break;
            case 3: // 单曲循环
                icon = 'fa-retweet';
                title = '单曲循环';

                model     = 1; 
                nextIcon  = 'fa-exchange';
                nextTitle = '列表循环';
                break;
        }

        myplayer.model = model;
        $(this).removeClass(icon);
        $(this).addClass(nextIcon);
        $(this).attr('title',nextTitle);

        $('.arguments-type').prev().removeClass(icon);
        $('.arguments-type').prev().addClass(nextIcon);
        $('.arguments-type').text(nextTitle);
    });

    /* 鼠标左键按下 */
    $('.drag').mousedown(function(params)
    {
        myplayer.volumeMouse = true;
        $(this).addClass('current');
        document.onselectstart = function(){ event.returnValue = false; } // 禁止鼠标选择事件
        // console.log('音量按下');
    });

    /* 鼠标左键放开 */
    $(document).mouseup(function()
    {
        if(myplayer.volumeMouse)
        { 
            // 音量按钮 - 放开
            myplayer.volumeMouse = false;
            $('.drag').removeClass('current');
            document.onselectstart = null; // 取消鼠标选择事件
        }
    });

    /* 鼠标移动 */
    $(document).mousemove(function(point)
    {
        if(myplayer.volumeMouse){ // 调节音量
            var address = 0;
            var barlong = 145;//$('.volume-bar').css('width'); // 音量条宽度
            address = point.pageX - $('.progress').offset().left;

            if(address>=0 && address<=barlong)
            {
                myplayer.volume = address/barlong;
                // $('.drag').css('left',address-7+'px');      // 设置音量按钮位置
                $('.progress').css('width',address+'px');   // 设置音量选中背景长度

                $('.btn-trumpet').removeClass('fa-volume-off');
                $('.btn-trumpet').addClass('fa-volume-up');
                // console.log('音量：' + address/barlong);
            }
            else if(address<=0)
            {
                if(myplayer.volume!=0)
                {
                    myplayer.volume = 0;
                    // $('.drag').css('left','-7px');      // 设置音量按钮位置
                    $('.progress').css('width','0px');  // 设置音量选中背景长度

                    $('.btn-trumpet').removeClass('fa-volume-up');
                    $('.btn-trumpet').addClass('fa-volume-off');                    
                }
            }
            else if(address>=barlong)
            {
                if(myplayer.volume!=1)
                {
                    myplayer.volume = 1;
                    // $('.drag').css('left','138px');     // 设置音量按钮位置
                    $('.progress').css('width','145px');    // 设置音量选中背景长度
                }
            }
        }
    });

    /* 点击进度条 */
    $('.progress-bar').click(function(e)
    {
        var nowlong = myplayer.duration / 365 * e.offsetX
        myplayer.currentTime = nowlong;
    });

    // 按钮 - 喜欢
    $('#an .control-heart').click(function()
    {

    });

    /* 按钮 - 列表按钮 */
    $('#an #btn-music-list').click(function()
    {
        var has = $(this).hasClass('active');

        if( has ){
            $(this).removeClass('active');
            $('.an-list').removeClass('active');
            // $('.an-list .list-block').removeClass('active');

        }else{
            $(this).addClass('active');
            $('.an-list').addClass('active');
            // $('.an-list .list-block').addClass('active');
        }
    });

    // 按钮 - 歌词开关
    $('#an #btn-lrc').click(function(){
    });

    /* 歌曲列表 - 播放 */
    $('#an .list-music').delegate( 'li', 'click', function(evt)
    {
        $('#an .list-music .active').removeClass('active');
        $(this).addClass('active');
        var index = Number($(this).children('span').eq(0).text()) - 1;
        var music = Storage.getItem( 'music' );
        myplayer.src = music[index].link;
        console.log(myplayer);
        myplayer.play();
    });

    /* 点击专辑 */
    $('#an .list-album').delegate( 'li', 'click', function()
    {
        $('#an .list-album').removeClass('active');
        $('#an .list-music').addClass('active');
        var index = Number($(this).children('span').eq(0).text()) - 1;
        var album = Storage.getItem( 'album' );
        $.getJSON('http://api.ziiber.me/Music/lists/album_id/' + album[index].id + '?callback=?', function(data){
            if( data.code == 0 )
            {
                Storage.setItem( 'music', data.value );
                addItem( data.value, 1 );
            }
        });
    });

    // 歌曲列表 - 返回
    $('#an .list-music>.block-title').click( function()
    {
        $('#an .list-music').removeClass('active');
        $('#an .list-album').addClass('active');
    });

    /* 事件 - 播放回调 */
    myplayer.listenEvent( 'play', function(e)
    {
        // 设置总时间
        $('.arguments-total').text(myplayer.time_s2m(Math.floor(myplayer.duration)));
        
        console.log('开始播放');
    });

    /* 事件 - 暂停回调 */
    myplayer.listenEvent( 'pause', function()
    {

        console.log('播放暂停');
    });

    /* 事件 - 时间进度 */
    myplayer.listenEvent( 'timeupdate', function(s)
    {
        if( myplayer.currentTime < myplayer.duration )
        {
            var nowBar = myplayer.currentTime / myplayer.duration * 100;
            $('.arguments-time').text(myplayer.time_s2m(Math.floor(myplayer.currentTime)));
            $('.progress-bar .current').css('width',nowBar+'%');
        }
        else if( myplayer.currentTime >= myplayer.duration )
        {

            $('.arguments-time').text('00:00');
            $('.arguments-total').text('00:00');
            $('.progress-bar .current').css('width','0');

            console.log('播放结束');
        }
    });

    $('#an .list-block .block-content').niceScroll( 
        {
            cursorwidth:9,
            cursorcolor:'#000',
            horizrailenabled:false,
            cursorborder:'0px', // 边框
            hidecursordelay:600, // 淡出时间
            visibilitycursordelay:600, // 淡出时间
            scrollspeed:250, // 滚动速度
            mousescrollstep:40, //鼠标滚动间距
            railoffset:{left:-2}, // 左偏移
            cursoropacitymin:0,
            cursoropacitymax:0.3,
            railpadding:{ left:1,right:1 },
            sensitiverail:true // 点击轨道滚动
        }
    );

    // 获取专辑列表
    $.getJSON('http://api.ziiber.me/Album/lists?callback=?', function(data){

        if( data.code == 0 )
        {
            Storage.setItem( 'album', data.value );
            addItem( data.value, 2 );
        }
    });
});

function addItem( list, type )
{
    // type 1=歌曲  2=专辑
    var type = arguments[1] ? arguments[1]:1;
    var li = [];
    var content = '';

    var number = list.length; // 行数

    var total = number * 41; // 总高度
    var showH = 310; // 显示区域高度
    var barH = 310; // 滚动条高度

    $.each( list, function( key, value )
    {
        var item = '';
        item = '<li>';
        if( type == 2 )
        item += '<i class="fa fa-angle-right"></i>';
        item += '<span class="list-taxis">' + (key+1) + '</span>';
        item += '<span class="list-name">' + value.name + '</span>';
        item += '</li>';

        li.push(item);
    });

    content = '<ul>' + li.join('') + '</ul>';

    if( total > showH )
    {
        var ratio = showH / total; // 比例
        var bar_height = ratio * barH;
        // content += '<div class="scroll-bar"><div class="bar" style="height:' + bar_height.toFixed(2) + 'px;"></div></div>';
    }

    if( type == 2 )
    {
        $('#an .list-album .block-content').html( content );
    }
    else
    {
        $('#an .list-music .block-content').html( content );
    }
}
